package persistance.database.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import persistance.hibernate.InitSessionFactory;
import domain.valueObejcts.ProjectPosition;

public class ProjectPositionDAO {

	private static Session session;
	
	public static List<ProjectPosition> create(List<ProjectPosition> positions) {
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		for (ProjectPosition pos : positions) {
			session.save(pos);
		}
		tx.commit();
		return positions;
	}

	@SuppressWarnings("unchecked")
	public static List<ProjectPosition> read(HashMap<String, String> filter) throws ParseException {
		List<ProjectPosition> result = new ArrayList<ProjectPosition>();
		List<String> list = new ArrayList<String>();
		
		for (Map.Entry<String, String> entry : filter.entrySet()){
			String column = entry.getKey();
			if(column.equals("name")){
				list.add(column + " LIKE CONCAT('%', :" + column + ", '%')");
			} else {
				list.add(column + " = :" + column);
			}
		}
		
		StringBuilder sb = new StringBuilder();
		if(filter.size() > 0){
			sb.append("WHERE ");
			sb.append(list.remove(0));
			for (String s : list) {
				sb.append(" AND ");
				sb.append(s);
			}
		}
		
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery("FROM ProjectPosition " + sb.toString() + " ORDER BY date ASC");
		
		for (Map.Entry<String, String> entry : filter.entrySet()){
			String column = entry.getKey();
			Object value = null;
			if(column.equals("date")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				value = sdf.parse(entry.getValue());
			} else {
				value = entry.getValue();
			}
			query.setParameter(column, value);
		}
		
		result = query.list();
		tx.commit();
		
		filter.clear();
		
		return result;
	}
	
	public static List<ProjectPosition> update(List<ProjectPosition> positions) {
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		for (ProjectPosition pos : positions) {
			session.update(pos);
		}
		tx.commit();
		return positions;
	}
	
	public static ProjectPosition update(ProjectPosition position) {
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		session.update(position);
		tx.commit();
		return position;
	}

	public static void delete(ProjectPosition position) {
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		session.delete(position);
		tx.commit();
	}

	public static void create(ProjectPosition pos) {
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		session.save(pos);
		tx.commit();
	}


}
